package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Study;
import cn.edu.nit.sys.mapper.StudyMapper;
import cn.edu.nit.sys.service.StudyService;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

/**
 * @Author 罗文斌
 * @Date 2023/5/6
 * @Version 1.0.0
 */
@Service
public class StudyServiceImpl extends ServiceImpl<StudyMapper, Study> implements StudyService {

    @Override
    public R getStudyList(Page<Study> page, String siftName) {
        Page<Study> list = baseMapper.getStudyList(page, siftName);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", list.getTotal());
        jsonObject.put("list", list.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    public R getDetail(String id) {
        Study detail = baseMapper.getDetail(id);
        return R.ok(detail);
    }

    @Override
    public R addStudyLog(Map<String, String> params) {
        String sid = params.get("sid");
        String uid = params.get("uid");
        String seat = params.get("seat");
        String startTime = params.get("startTime") + " 00:00:00";
        String endTime = params.get("endTime") + " 23:59:59";
        if(StringUtils.isNotEmpty(baseMapper.isExist(uid))) {
            return R.error("请勿重复占座");
        }
        String id = UUID.randomUUID().toString().replaceAll("\\-", "");
        int rs = baseMapper.addStudyLog(id, sid, uid, seat, startTime, endTime);
        return rs > 0 ? R.ok("占座成功") : R.error("占座失败");
    }

    @Override
    public R getLogList(Page<Map<String, String>> page, String uid) {
        Page<Map<String, String>> logList = baseMapper.getLogList(page, uid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", logList.getTotal());
        jsonObject.put("list", logList.getRecords());
        return R.ok(jsonObject);
    }
}
